﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "BindToFamilyParamDialog.h"
#include "IFamilyManager.h"
#include "IFamilyParameterDefinition.h"
#include "IParameter.h"
#include "ParameterType.h"

#include <QPushButton>

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace gfam;
BindToFamilyParamDialog::BindToFamilyParamDialog(QWidget *pParent) : QDialog(pParent)
{
    ui.setupUi(this);
    ui.buttonBox->button(QDialogButtonBox::Ok)->setText(QString::fromStdWString(GBMP_TR(L"确定")));
    ui.buttonBox->button(QDialogButtonBox::Cancel)->setText(QString::fromStdWString(GBMP_TR(L"取消")));
}

BindToFamilyParamDialog::~BindToFamilyParamDialog()
{

}

void BindToFamilyParamDialog::InitDialog(const IParameter* curParam, Int32 bindParamDefId, 
    gfam::IFamilyManager* pFamManager)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(curParam, L"param is null.",L"GDMPLab",L"2024-03-30");
    m_pFamManager = pFamManager;
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_pFamManager != nullptr, L"pFamManager为空",L"GDMPLab",L"2024-03-30");
    ui.label_ParamName->setText(QString::fromStdWString(curParam->GetParameterDefinition()->GetName()));
    ui.label_ParamType->setText(GetParameterTypeName(curParam));
    ui.listWidget_FamParams->blockSignals(true);
    OwnerPtr<QListWidgetItem> opItem = NEW_AS_OWNER_PTR(QListWidgetItem, ui.listWidget_FamParams);
    opItem->setText(QString::fromStdWString(GBMP_TR(L"<无>")));
    opItem->setData(Qt::UserRole, -1);
    ui.listWidget_FamParams->addItem(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opItem));
    const IParameterDefinition* curParamDef = curParam->GetParameterDefinition();
    std::vector<ElementId> allParams = m_pFamManager->GetFamilyParameterDefinitionIds();
    int curBindindex = 0;
    FOR_EACH(idParam, allParams)
    {
        const IFamilyParameterDefinition *pParamDef = m_pFamManager->GetFamilyParameterDefinition(idParam);
        if(pParamDef != nullptr
            && pParamDef->GetParameterTypeId() == curParamDef->GetParameterTypeId()
            && pParamDef->GetStorageType() == (ParameterStorageType)curParamDef->GetStorageType()
            && pParamDef->GetUnitTypeId() == curParamDef->GetUnitTypeId())
        {
            Int32 paramDefId = m_pFamManager->GetFamilyParameterDefinitionId(pParamDef->GetElementId());
            OwnerPtr<QListWidgetItem> opItem = NEW_AS_OWNER_PTR(QListWidgetItem, ui.listWidget_FamParams);
            opItem->setText(QString::fromStdWString(pParamDef->GetName()));
            opItem->setData(Qt::UserRole, paramDefId);
            ui.listWidget_FamParams->addItem(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opItem));
            if(paramDefId == bindParamDefId)
            {
                curBindindex = ui.listWidget_FamParams->count() - 1;
            }
        }
    }
    ui.listWidget_FamParams->setCurrentRow(curBindindex);
    ui.listWidget_FamParams->blockSignals(false);
}

QString BindToFamilyParamDialog::GetParameterTypeName(const IParameter* curParam)
{
    DBG_WARN_AND_RETURN_UNLESS(curParam, QString::fromStdWString(L""), L"param is null.",L"GDMPLab",L"2024-03-30");

    std::wstring strParamType;
    UniIdentity ptId = curParam->GetParameterDefinition()->GetParameterTypeId();
    if (ptId == PARAMETER_TYPE(Angle))
    {
        strParamType = GBMP_TR(L"角度");
    }
    else if (ptId == PARAMETER_TYPE(Length))
    {
        strParamType = GBMP_TR(L"长度");
    }
    else if (ptId == PARAMETER_TYPE(Material))
    {
        strParamType = GBMP_TR(L"材质");
    }
    else if (ptId == PARAMETER_TYPE(Number))
    {
        strParamType = GBMP_TR(L"整型");
    }
    else if (ptId == PARAMETER_TYPE(Float))
    {
        strParamType = GBMP_TR(L"数值");
    }
    return QString::fromStdWString(strParamType);
}

Int32 BindToFamilyParamDialog::GetBoundParamDefId()
{
    if(ui.listWidget_FamParams->currentRow() == 0)
    {
        return -1;
    }
    return ui.listWidget_FamParams->currentItem()->data(Qt::UserRole).toInt();
}